class Solution
{
public:
    string findLongestWord(string s, vector<string>& dictionary)
    {
        sort(dictionary.begin(), dictionary.end(), [](string& a, string& b)
            {
                return a.size() == b.size() ? a < b : a.size() > b.size();
            });

        int n = s.size();
        for (auto& ss : dictionary)
        {
            int m = ss.size();

            if (m > n)   continue;

            int i = 0, j = 0;
            while (i < n && j < m)
            {
                if (s[i] == ss[j])
                    j++;
                i++;
            }
            if (j == m)
                return ss;
        }
        return "";
    }
};